﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visa.DB;

namespace Visa.Service
{
    public class UserService
    {
        private VisaEntities db = new VisaEntities();

        public bool DeleteRoles(string userName)
        {
            try
            {
                var CategotyToDetach = db.Users.SingleOrDefault(o => o.UserName == userName);
                CategotyToDetach.Deleted = true;
                db.SaveChanges();

                return true;
            }
            catch { return false; }
        }

        public bool DeleteUserByID(int id)
        {
            try
            {
                var CategotyToDetach = db.Users.SingleOrDefault(o => o.UsID == id);
                CategotyToDetach.Deleted = true;
                db.SaveChanges();

                return true;
            }
            catch { return false; }
        }

        public void UpdateStausUser(int id, bool TTs)
        {
            var CategotyToDetach = db.Users.SingleOrDefault(o => o.UsID == id);
            CategotyToDetach.IsActive = TTs;
            db.SaveChanges();
        }

        public bool Kichhoaitaikhoan(string email)
        {
            try
            {
                var c = db.Users.SingleOrDefault(o => o.UserEmail == email);
                if (c != null)
                {
                    c.IsActive = true;
                    db.SaveChanges();
                    return true;
                }
                else return false;
            }
            catch { return false; }
        }

        public bool KichhoaitaikhoanDangKy(int email)
        {
            try
            {
                var c = db.Users.SingleOrDefault(o => o.UsID == email);
                if (c != null)
                {
                    c.IsActive = true;
                    db.SaveChanges();
                    return true;
                }
                else return false;
            }
            catch { return false; }
        }

        public List<User> GetAllRoles(bool showHidden = false)
        {
            try
            {
                if (showHidden == true)
                {
                    var c = from i in db.Users
                            where i.Deleted == false
                            select i;
                    return c.ToList();
                }
                else
                {
                    var c = from i in db.Users
                            where i.Deleted == false && i.IsActive == true
                            orderby i.DateCreate descending
                            select i;
                    return c.ToList();
                }

            }

            catch { return null; }
        }

        public User GetRolesById(int categoryId)
        {
            try
            {
                if (categoryId == -1)
                    return null;

                return db.Users.SingleOrDefault(l => l.UsID == categoryId);
            }
            catch { return null; }
        }

        public User InsertRoles(User category)
        {
            db.Users.Add(category);
            db.SaveChanges();
            return category;
        }

        public void UpdateRoles(User category)
        {
            var CategotyToDetach = db.Users.SingleOrDefault(o => o.UsID == category.UsID);
            CategotyToDetach.UserName = category.UserName;
            CategotyToDetach.UserEmail = category.UserEmail;
            CategotyToDetach.UserPass = category.UserPass;
            CategotyToDetach.UserFullName = category.UserFullName;
            CategotyToDetach.Birthday = category.Birthday;
            CategotyToDetach.Madel = category.Madel;
            CategotyToDetach.Phone = category.Phone;
            CategotyToDetach.IsActive = category.IsActive;
            db.SaveChanges();
        }

        public List<User> SearchCategory(string name)
        {
            try
            {
                var c = from i in db.Users
                        where i.Deleted == false
                        && i.UserName.Contains(name)
                        select i;
                return c.ToList();
            }
            catch { return null; }
        }

        public bool checkUserByEmail(string email)
        {
            try
            {
                var c = db.Users.SingleOrDefault(o => o.UserEmail == email);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public bool checkUserByUserName(string userName)
        {
            try
            {
                var c = db.Users.SingleOrDefault(o => o.UserName == userName);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public bool CheckUserLogin(string userName, string Pass)
        {
            try
            {
                var c = db.Users.SingleOrDefault(o => o.UserName == userName && o.UserPass == Pass);
                if (c != null && c.Deleted == false) return true;
                else return false;
            }
            catch { return false; }
        }

        public bool ChangePassword(string userName, string oldPass, string newPass)
        {
            try
            {
                var c = db.Users.SingleOrDefault(o => o.UserName == userName);
                c.UserPass = newPass;
                db.SaveChanges();
                return true;
            }
            catch { return false; }
        }

        public User GetUserByEmilAndUsName(bool type, string Ext)
        {
            if (type)
            {
                return db.Users.SingleOrDefault(o => o.UserName == Ext);
            }

            else
            {
                return db.Users.SingleOrDefault(o => o.UserEmail == Ext);
            }
        }

        public List<User> GetALlUserInRoles(int RoelsID)
        {
            try
            {
                var c = (from i in db.Roles
                         join m in db.UserInRoles on i.RoleID equals m.RolesID
                         join l in db.Users on m.UserID equals l.UsID
                         where (i.RoleID == RoelsID || RoelsID == -1)
                         && l.Deleted == false
                         select l);
                if (c != null) return c.ToList();
                else return null;
            }
            catch { return null; }
        }
        
    }
}
